<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
<script type="text/javascript">
	mystore.organStore.removeAll(true);
	var tree_parent_id ;
	var opr_type="";
	var page_param={};
	function pageRequest(){
		var url="";
		var record = null;
		if("add"==opr_type){
			url="organization!add.action";
		}else if("edit"==opr_type){
			url="organization!update.action";
		}else if("del"==opr_type){
			url="organization!delete.action";
			record=Ext.getCmp("org_grid").getSelectionModel().getSelected();
			page_param={
				id:record.data.id,
				supperId:record.data.supperId
			};
		}	
    	Ext.Ajax.request( {
    		url:url,
    		params:page_param,
    		success: function(response, opts){
				var d=eval("("+response.responseText+")");
				if(d.success){
					Ext.Msg.show({
						   title:"提示",
						   msg:"操作成功！",
						   buttons:Ext.Msg.OK,
						   icon: Ext.MessageBox.INFO
					});
					if("del"==opr_type){
						mystore.organStore.remove(record);
						Ext.getCmp("org_tree").getNodeById(page_param.id).remove(true);
					}else{
						Ext.getCmp("org_paging").doRefresh();
						Ext.getCmp("org_tree").getNodeById(page_param.supperId).reload();
					}
					
					Ext.getCmp("org_paging").doRefresh();
					Ext.getCmp("org_panel").layout.setActiveItem(0);
				} else {
					Ext.Msg.show({
						   title:"提示",
						   msg:"操作失败！",
						   buttons:Ext.Msg.OK,
						   icon: Ext.MessageBox.ERROR
					});
				}
				Ext.getCmp("save").setDisabled(false);
			},
			failure: function(response, opts){
				
			}
    	});
	}
	
	var org_panel = {
		id: "org_grid",
		xtype: "grid",
		stripeRows:true,
		loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
		autoExpandColumn:"address",//自动填充
		autoExpandMin : 100,//自动填充最小宽度
		store:mystore.organStore,
		headersDisabled: true,
		columns:[
        	new Ext.grid.RowNumberer({header: "序号", width: 35}),
        	{header: "机构号", dataIndex: "id", menuDisabled: true},
        	{header: "机构名称", dataIndex: "name", menuDisabled: true},
        	{header: "机构类型", dataIndex: "type", menuDisabled: true,
        		renderer:function(input){
        		if(""==input)return input;
         		return mystore.organTypeStore.getById(input).data.text;
        	}},
			{header: "联系人", dataIndex: "contact", menuDisabled: true},
			{header: "电话", dataIndex: "phone", menuDisabled: true},
			{header: "邮编", dataIndex: "post", menuDisabled: true},
			{header: "地址", dataIndex: "address",id:"address", menuDisabled: true}
		],
		bbar: new Ext.ux.PagingToolbar({
			id:"org_paging",
        	pageSize: limit,
        	store:mystore.organStore
    	}),
    	buttonAlign:"center",
		buttons:[
		{text:"新增", handler: function(){
			var node = Ext.getCmp("org_tree").getSelectionModel().getSelectedNode();
			if(node==null){
				Ext.Msg.show({
					   title:"提示",
					   msg: "请先选择机构节点！",
					   buttons: Ext.Msg.OK,
					   animEl: "elId",
					   icon:Ext.MessageBox.WARNING
					});
				return;
			}else{
				var orgNo = node.id;
				Ext.Ajax.request({
					params: {'orgNo':orgNo},
					url: 'organization!queryType.action',
					success: function(response){
						var dl = eval("("+response.responseText+")");
						if("O"==dl.orgType){
							Ext.Msg.show({
								title: "提示",
								msg: "分理处不能再增加下级机构！",
								buttons: Ext.Msg.OK,
								icon: Ext.MessageBox.WARNING
							});
							return;
						}else if("H"==dl.orgType){
							Ext.getCmp("orgtype").setValue('B');
						}else if("B"==dl.orgType){
							Ext.getCmp("orgtype").setValue('S');
						}else if("S"==dl.orgType){
							Ext.getCmp("orgtype").setValue('O');
						}
						
					}
				});
			}
			opr_type = "add";
			var panel = this.ownerCt.ownerCt.ownerCt;
			var addOrg = panel.getComponent("manager_org");
			addOrg.resetParam();
			panel.layout.setActiveItem(1);
		}},{text:"修改", handler: function(){
			var node=Ext.getCmp("org_tree").getSelectionModel().getSelectedNode();
			var selected=Ext.getCmp("org_grid").getSelectionModel().getSelections();
			if(null==node || ""==selected.join("")){
				Ext.Msg.show({
					title: "提示",
					msg: "请选择要修改的机构！",
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.WARNING
				});
				return;
			}
			opr_type="edit";
			var updateOrg = this.ownerCt.ownerCt;
			var panel = updateOrg.ownerCt;
			var record = updateOrg.getSelectionModel().getSelected();
			if (record){
				Ext.getCmp("manager_org").setParam(record.data);
				panel.layout.setActiveItem(1);
			}
		}},{text:"删除", handler: function(){
			var node = Ext.getCmp("org_tree").getSelectionModel().getSelectedNode();
			var selected = Ext.getCmp("org_grid").getSelectionModel().getSelections();
			if(null==node || ""==selected.join("")){
				Ext.Msg.show({
					title: "提示",
					msg: "请先选择要删除的机构！",
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.WARNING
				});
				return;
			}
			opr_type="del";
			Ext.Msg.show({
			   title:'提示',
			   msg:'是否确认删除该数据?',
			   buttons: Ext.Msg.OKCANCEL,
			   fn:function(btn, text){
				    if (btn == 'ok'){
				    	Ext.Ajax.request({
				    		params: {start:0,limit:10,'depart.oid':Ext.getCmp("org_grid").getSelectionModel().getSelected().id},
				    		url: 'depart!listByOrg.action',
				    		success: function(response){
				    			var dl = eval("("+response.responseText+")");//把返回的字符串转换成对象
				    			if(dl.total>0){
				    				Ext.Msg.show({
				    					title: "提示",
										msg: "机构下面有部门，不能删除！",
										buttons: Ext.Msg.OK,
										icon: Ext.MessageBox.WARNING
				    				});
				    				return;
				    			}
				    			pageRequest();
				    		}
				    	});  
				    }
			   },
			   animEl: 'elId',
			   icon: Ext.MessageBox.WARNING
			});
		}
		}]
	};
	var org_add_update = 0;
	//--------------------------------------------------------------------
	var manager_org = {
		id: "manager_org",
	    labelWidth: 60,
		xtype: "form",
	    bodyStyle:"padding:5px 5px 0",
		defaultType: "textfield",
		items: [{
			itemId: "org_no",
			fieldLabel: "机构号",
			anchor:"100%",
			disabled:true,
			name: "id"
		},{
			itemId: "org_name",
			fieldLabel: "机构名",
			anchor:"100%",
			name: "name",
			vtype:"alphanumchinese",
			validator:function (val){
        		if(val=="")return "该输入项为必输项！";
        		if(Ext.fucCheckCHLength(val)>50){
        			return "请输入50个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
        		}
        		return true;
        	}
		},{
			id: "orgtype",
			fieldLabel: "机构类型",
			allowBlank:false,
			disabled:true,
			xtype: 'billCombox',
			anchor:"100%",
			store: mystore.organTypeStore,
			valueField: "id",
			displayField: "text",
			name: "type"
		},{
			itemId: "area",
			fieldLabel: "区域",
			allowBlank:false,
			anchor:"100%",
			name: "area",
			vtype:"alphanumchinese",
			validator:function (val){
        		if(val=="")return "该输入项为必输项！";
        		if(Ext.fucCheckCHLength(val)>20){
        			return "请输入20个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
        		}
        		return true;
        	}
		},{
			itemId: "org_contact",
			anchor:"100%",
			fieldLabel: "联系人",
			anchor:"100%",
			name: "contact",
			vtype:"alphanumchinese",
			validator:function (val){
        		if(val=="")return true;
        		if(Ext.fucCheckCHLength(val)>20){
        			return "请输入20个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
        		}
        		return true;
        	}
		}, {
			itemId: "org_phone",
			fieldLabel: "电话",
			xtype:"numberfield",
			anchor:"100%",
			name: "phone",
			maxValue:99999999999999999999
			
		}, {
			itemId: "org_post",
			xtype:"numberfield",
			fieldLabel: "邮编",
			anchor:"100%",
			name: "post",
			maxValue:999999
		}, {
			itemId: "org_address",
			fieldLabel: "地址",
			anchor:"100%",
			vtype:"alphanumchinese",
			name: "address",
			validator:function (val){
        		if(val=="")return true;
        		if(Ext.fucCheckCHLength(val)>200){
        			return "请输入200个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
        		}
        		return true;
        	}
		}],
		buttonAlign:"center",
		buttons:[
			{text:"保存",id:"save", handler: function(e){
				if(!Ext.getCmp("manager_org").getForm().isValid()){
					//Ext.Msg.alert("提示","验证不通过！");
					Ext.Msg.show({
						   title:"警告",
						   msg: "验证不通过",
						   buttons: Ext.Msg.OK,
						   animEl: "elId",
						   icon:Ext.MessageBox.WARNING
						});
					return;
				}
				var p2 = this.ownerCt.ownerCt;
				var org_no = p2.getComponent("org_no").getValue(),
					org_name = p2.getComponent("org_name").getValue(),
					org_type = p2.getComponent("orgtype").getValue(),
					area = p2.getComponent("area").getValue(),
					org_contact = p2.getComponent("org_contact").getValue(),
					org_phone = p2.getComponent("org_phone").getValue(),
					org_post = p2.getComponent("org_post").getValue(),
					org_address = p2.getComponent("org_address").getValue();
				
				var node=Ext.getCmp("org_tree").getSelectionModel().getSelectedNode();
				if(node==null){
					//Ext.Msg.alert("提示","请选择上级机构！");
					Ext.Msg.show({
						   title:"警告",
						   msg: "请选择上级机构!",
						   buttons: Ext.Msg.OK,
						   animEl: "elId",
						   icon:Ext.MessageBox.WARNING
						});
					return;
				}
				page_param={
						id:org_no,//机构ID
						name:org_name,//机构名称
						supperId:node.id,
						type:org_type,//机构类型
						contact:org_contact,//
						area:area,//所在区域
						phone:org_phone,//电话
						post:org_post,//邮编
						address:org_address//地址
				};
				
				e.setDisabled(true);
				pageRequest();
				Ext.getCmp("org_panel").layout.setActiveItem(0);
			}},{text:"取消", handler: function(){
				this.ownerCt.ownerCt.ownerCt.layout.setActiveItem(0);
			}}
		],
		setParam: function(data){
			var me = this;
			me.getComponent("org_no").setValue(data.id);
			me.getComponent("org_no").show();
			me.getComponent("org_name").setValue(data.name);
			me.getComponent("orgtype").setValue(data.type);
			me.getComponent("area").setValue(data.area);
			me.getComponent("org_contact").setValue(data.contact);
			me.getComponent("org_phone").setValue(data.phone);
			me.getComponent("org_post").setValue(data.post);
			me.getComponent("org_address").setValue(data.address);
			org_add_update = 1;
		},
		resetParam: function(){
			var me = this;
			me.getComponent("org_no").setValue("");
			me.getComponent("org_no").hide();
			me.getComponent("org_name").setValue("");
			me.getComponent("area").setValue("");
			me.getComponent("org_contact").setValue("");
			me.getComponent("org_phone").setValue("");
			me.getComponent("org_post").setValue("");
			me.getComponent("org_address").setValue("");
		}
	};
	
	var org_pel=new Ext.Panel({
		id:"master_panel",
		border : false, 
		layout: "border",
		items: [{
				id:"org_tree",
				xtype: "billTreePanel",
				dataUrl: "tree!organization.action",
				root: new Ext.tree.AsyncTreeNode({
					id: "root",
					expanded : true		// 自动展开节点
				}),
				listeners: {
					"expandnode": function(node){
						mystore.organStore.baseParams["parentId"] = node.id;
						mystore.organStore.load({params:{start : 0,limit : limit}});
						Ext.getCmp("org_panel").layout.setActiveItem(0);
					}
				}
			}, {
				id: "org_panel",
				region: "center",
				border : false, 
				layout:"card",
				activeItem:0,//显示第一个容器
				items: [org_panel,manager_org]
		}]
	});
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(org_pel);
	CenterPanel.doLayout();
</script>